//
//  MenuViewController.m
//  testControlView
//
//  Created by Le Thanh Hai on 10/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MenuViewController.h"
const float gBarHeight = 30.0f;

@implementation MenuViewController
@synthesize delegate;
@synthesize contentHeight = _contentHeight;
@synthesize open = _isOpen;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _isOpen = FALSE;
		_contentHeight = frame.size.height - gBarHeight;
		
		// Gradient setup for bar
		float barColors[8] = {
			0.30f, 0.30f, 0.30f, 0.75f,
			0.00f, 0.00f, 0.00f, 0.75f
		};
		CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
		if (colorSpace != NULL)
		{
			_barGradient = CGGradientCreateWithColorComponents(colorSpace, barColors, NULL, 2);
			if (_barGradient == NULL)
				NSLog(@"Failed to create CGGradient");
			CGColorSpaceRelease(colorSpace);
		}
		_barStartPoint = CGPointMake(0.0f, 0.0f);
		_barEndPoint = CGPointMake(0.0f, gBarHeight);
		
		// Image setup for bar
		UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Triangle" ofType:@"png"]];
		if (image == nil)
			NSLog(@"Failed to load image for control vieww");
		
		_barImageView = [[[UIImageView alloc] initWithImage:image] autorelease];
		CGRect imageViewFrame = _barImageView.frame;
		imageViewFrame.origin.x = floorf((frame.size.width/2.0f) - (imageViewFrame.size.width/2.0f));
		imageViewFrame.origin.y = floorf((gBarHeight/2.0f) - (imageViewFrame.size.height/2.0f));
		[_barImageView setFrame:imageViewFrame];
		
		[self addSubview:_barImageView];
		
		_barImageViewRotation = CGAffineTransformMake(cos(-M_PI), sin(-M_PI), -sin(-M_PI), cos(-M_PI), 0.0f, 0.0f);
        
		self.opaque = FALSE;

    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSaveGState(context);
	
	CGContextDrawLinearGradient(context, _barGradient, _barStartPoint, _barEndPoint, kCGGradientDrawsAfterEndLocation);
	
	CGContextSetRGBFillColor(context, 0.25f, 0.25f, 0.25f, 1.0f);
	UIRectFill(CGRectMake(0.0f, 0.0f, self.frame.size.width, 1.0f));
	
	CGContextRestoreGState(context);
}

+ (CGFloat)barHeight
{
	return gBarHeight;
}

- (void)dealloc
{
	CGGradientRelease(_barGradient);
	
    [super dealloc];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch;
	CGPoint point;
	
	if ([touches count] > 0)
	{
		touch = [[touches allObjects] objectAtIndex:0];
		point = [touch locationInView:self];
		
		if (point.y <= gBarHeight)
		{
			[UIView beginAnimations:nil context:NULL];
			[UIView setAnimationDuration:0.15f];
            
			CGRect frame = [self frame];
			
			if (_isOpen == FALSE)
			{
				frame.origin.y -= _contentHeight;
				[_barImageView setTransform:_barImageViewRotation];
				_isOpen = TRUE;
			}
			else
			{
				frame.origin.y += _contentHeight;
				[_barImageView setTransform:CGAffineTransformIdentity];
				_isOpen = FALSE;
			}
			
			[self setFrame:frame];
			
			[UIView commitAnimations];
		}
	}
    
    /****************/
    if(delegate && [delegate respondsToSelector:@selector(touchView:)]) {
        [delegate touchView:nil];
    }
}

@end
